<?php

namespace MarketServer\Controller;

/**
 * 公告控制器
 *
 * @since 1.0 2015-8-7 by sutroon
 */
class NoticesController extends AppbaseController {
    
    /**
     * 文章列表页面
     * @since 1.0 <2015-8-7> SoChishun Refacting.
     */
    public function notices_list() {
        if(!$this->validate_permission('WZ_M3_GGGL',$msg)){
            die($msg);
        }
        $this->display();
    }

    public function notices_edit($id = 0) {
        if ($id) {
            $data = D('Notices')->find($id);
            $this->assign('data', $data);
        }
        $this->display();
    }
    
    public function notices_catagory($id){
        $this->assign('data',D('Notices')->find($id));
        $this->display();
    }

    /**
     * 获取列表查询数据
     * @return array
     * @since 1.0 <2015-7-2> SoChishun Added.
     */
    function get_notices_search_data() {
        $search = I();
        if ($search['create_time']) {
            $where['create_time'] = array('EGT', $search['create_time']);
        }
        if ($search['user_name']) {
            $where['user_name'] = $search['user_name'];
        }
        return array('search' => $search, 'where' => $where);
    }

    /**
     * 导出数据
     * @since 1.0 <2014-6-13> SoChishun Added.
     */
    public function notices_export() {
        if(!$this->validate_permission('WZ_M3_GGGL',$msg)){
            die($msg);
        }
        $search_data = $this->get_notices_search_data();
        $list = D('Notices')->scope('export')->where($search_data['where'])->select();
        if (!$list) {
            $this->ajaxMsg(false, '找不到符合条件的记录!');
        }
        sofn_excel_export($list);
    }

    /**
     * 获取列表JSON数据
     * @since 1.0 <2015-6-10> SoChishun Added.
     */
    public function get_notices_list_json() {
        $search_data = $this->get_notices_search_data();
        $list = D('Notices')->get_notices_list($search_data['where']);
        $this->ajaxReturn($list);
    }

    /**
     * 保存文章
     * @since 1.0 <2015-6-11> SoChishun Added.
     */
    public function save_notices_json() {
        if(!$this->validate_permission('WZ_M3_GGGL',$msg)){
            die($msg);
        }
        $msg = D('Notices')->save_notices($this->user_login_data['user_name']);
        if (!$msg['status'] && is_array($msg['info'])) {
            $msg['info'] = implode("<br />", $msg['info']);
        }
        $this->ajaxReturn($msg);
    }

    /**
     * 保存文章类别
     * @since 1.0 <2015-8-4> SoChishun Added.
     */
    public function save_notices_category() {
        if(!$this->validate_permission('WZ_M3_GGGL',$msg)){
            die($msg);
        }
        $id = I('id');
        $role_id = I('role_id');
        if(!$id || !$role_id){
            $this->ajaxMsg(false,'参数有误');
        }
        $result = M('tnotices')->where(array('id' => $id))->setField('category_id', $role_id);
        $this->ajaxMsg($result);
    }

    /**
     * 删除文章
     * @param string $id 主键编号,多个之间以逗号隔开
     * @since 1.0 <2015-6-10> SoChishun Added.
     */
    public function notices_delete($id = '') {
        if(!$this->validate_permission('WZ_M3_GGGL',$msg)){
            die($msg);
        }
        if (!$id) {
            $this->ajaxMsg(false, '编号无效');
        }
        $msg = D('Notices')->notices_delete($id, $this->user_login_data['user_name']);
        $this->ajaxReturn($msg);
    }
}
